﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace e2
{
    public class OrderService
    {
        public List<Customer> customers = new List<Customer>
        {
            new Customer("李威", "854201", "武汉大学"),
            new Customer("张子健", "853201", "武汉大学")
        };
        public List<Product> products = new List<Product>
        {
            new Product("LW的外衣",20),
            new Product("LW的鞋子",10),
            new Product("LW的围巾",30),
            new Product("LW的steam号",50),
            new Product("LW",5)
        };
        public List<Order> orders = new List<Order>();

        public OrderService(List<Order> orders)
        {
            this.orders = orders;
        }

        public OrderService()
        {
            orders = new List<Order>
            {
                new Order(new List<Good>{new Good(products[4],3)},new OrderDetails("1",customers[0], new DateTime(2022, 3, 19, 18, 12, 34))),
                new Order(new List<Good>{new Good(products[1],2)},new OrderDetails("2",customers[1], new DateTime(2022, 3, 19, 18, 12, 37)))
            };

        }

        public bool IfExist(Order order)
        {
            for (int i = 0; i < orders.Count; i++)
                if (orders[i].Equals(order))
                    return true;
            return false;
        }

        public int OrderAt(Order order)
        {
            for (int i = 0; i < orders.Count; i++)
                if (orders[i].Equals(order))
                    return i;
            return -1;
        }

        public void ShowAllCustomers()
        {
            //
            Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
            Console.WriteLine("现有客户： \n");
            for (int i = 0; i < customers.Count; i++)
                Console.WriteLine(i + "." + customers[i].ToStringWOD());
            Console.WriteLine("************************************************* \n");
        }
        public void ShowAllProducts()
        {
            //
            Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
            Console.WriteLine("现有产品： \n");
            for (int i = 0; i < products.Count; i++)
                Console.WriteLine(i + ". " + products[i]);
            Console.WriteLine("************************************************* \n");
        }
        public void ShowAllOrder()
        {
            Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
            for (int i = 0; i < orders.Count; i++)
                Console.WriteLine(i + ".\n" + orders[i]);
            Console.WriteLine("************************************************* \n");
        }
        public void AddMenu()
        {
            bool flag = true;
            while (flag)
            {
                Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
                Console.WriteLine("                1.添加客户\n");
                Console.WriteLine("                2.添加订单\n");
                Console.WriteLine("                3.添加商品\n");
                Console.WriteLine("                4.退出        \n");
                Console.WriteLine("************************************************* \n");
                Console.Write("您的选择是(请输入单个命令前的序号数字)：");
                char choose = Console.ReadKey(true).KeyChar;
                switch (choose)
                {
                    case '1': AddCustomer(); break;
                    case '2': AddOrder(); break;
                    case '3': AddProduct(); break;
                    case '4': flag = false; break;
                    default: break;
                }
            }
        }
        public void AddCustomer()
        {
            ShowAllCustomers();
            Customer customer = new Customer();
            Console.Write("客户姓名：");
            customer.Name = Console.ReadLine();
            Console.Write("客户住址：");
            customer.Address = Console.ReadLine();
            Console.Write("手机号码：");
            customer.PhoneNumber = Console.ReadLine();
            customers.Add(customer);
        }
        public void AddProduct()
        {
            ShowAllProducts();
            Product product = new Product();
            Console.Write("产品名称：");
            product.Name = Console.ReadLine();
            Console.Write("产品单价：");
            product.Price = int.Parse(Console.ReadLine());
            products.Add(product);
        }
        public void AddOrder()
        {
            bool flag = true;
            ShowAllCustomers();
            Console.Write("选择客户（选择其序号）：");
            int num = int.Parse(Console.ReadLine());
            Customer cus = customers[num];
            Console.Write("成功选择" + cus.Name + ",按任意键继续.....");
            Console.ReadKey(true);
            List<Good> gds = new List<Good>();
            while (flag)
            {
                ShowAllProducts();
                Console.WriteLine("************************************************* \n");
                Console.WriteLine("已购产品：");
                foreach (Good good in gds)
                    Console.WriteLine(good);
                Console.WriteLine("************************************************* \n");
                Console.Write("选择产品（选择其序号）：");
                num = int.Parse(Console.ReadLine());
                Console.Write("购买数量：");
                int i = int.Parse(Console.ReadLine());
                gds.Add(new Good(products[num], i));
                Console.Write("成功下单" + products[num].Name + ",按回车返回，否则继续添加.....");
                if (Convert.ToInt32(Console.ReadKey(true).KeyChar) == 13)
                    flag = false;
            }
            DateTime time = DateTime.Now;
            string id = (orders.Count + 1).ToString();

            Order order = new Order(gds, new OrderDetails(id, cus, time));
            if (IfExist(order))
                throw new Exception("订单已存在！操作失败......");
            else
                orders.Add(order);
        }

        public void ChangeMenu()
        {
            bool flag = true;
            while (flag)
            {
                //
                Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
                Console.WriteLine("                1.修改客户信息\n");
                Console.WriteLine("                2.修改订单\n");
                Console.WriteLine("                3.修改商品\n");
                Console.WriteLine("                4.退出        \n");
                Console.WriteLine("************************************************* \n");
                Console.Write("您的选择是(请输入单个命令前的序号数字)：");
                char choose = Console.ReadKey(true).KeyChar;
                switch (choose)
                {
                    case '1': ChangeCustomer(); break;
                    case '2': ChangeOrder(); break;
                    case '3': ChangeProduct(); break;
                    case '4': flag = false; break;
                    default: break;
                }
            }
        }
        public void ChangeCustomer()
        {
            ShowAllCustomers();
            Console.Write("选择要改变客户的序号：");
            int num = int.Parse(Console.ReadLine());
            Console.Write("客户姓名：");
            customers[num].Name = Console.ReadLine();
            Console.Write("客户住址：");
            customers[num].Address = Console.ReadLine();
            Console.Write("手机号码：");
            customers[num].PhoneNumber = Console.ReadLine();
            Console.Write("成功修改" + customers[num].Name + "的信息,按任意键继续.....");
            Console.ReadKey(true);
        }
        public void ChangeProduct()
        {
            ShowAllProducts();
            Console.Write("选择要改变商品的序号：");
            int num = int.Parse(Console.ReadLine());
            Console.Write("产品名称：");
            products[num].Name = Console.ReadLine();
            Console.Write("产品单价：");
            products[num].Price = int.Parse(Console.ReadLine());
            Console.Write("成功修改" + products[num].Name + "的信息,按任意键继续.....");
            Console.ReadKey(true);
        }
        public void ChangeOrder()
        {
            //
            ShowAllOrder();
            Console.Write("选择修改的订单（选择其序号）：");
            int Ordernum = int.Parse(Console.ReadLine());
            Console.Write("成功选择,按任意键继续.....");
            Console.ReadKey(true);

            ShowAllCustomers();
            Console.Write("选择客户（选择其序号）：");
            int num = int.Parse(Console.ReadLine());
            orders[Ordernum].Details.Customer = customers[num];
            Console.Write("成功选择" + customers[num].Name + ",按任意键继续.....");
            Console.ReadKey(true);
            bool flag = true;
            while (flag)
            {
                ShowAllProducts();
                Console.WriteLine("************************************************* \n");
                Console.WriteLine("已购产品：");
                for (int goodnum = 0; goodnum < orders[Ordernum].Goods.Count; goodnum++)
                    Console.WriteLine(goodnum + "." + orders[Ordernum].Goods[goodnum]);
                Console.WriteLine("************************************************* \n");
                Console.Write("选择修改的产品（选择其序号）：");
                int gdnum = int.Parse(Console.ReadLine());
                Console.Write("选择修改后的产品（选择其序号）：");
                num = int.Parse(Console.ReadLine());
                Console.Write("购买数量：");
                int i = int.Parse(Console.ReadLine());
                orders[Ordernum].Goods[gdnum] = new Good(products[num], i);
                Console.Write("成功修改" + products[num].Name + ",按回车返回，否则继续添加.....");
                if (Convert.ToInt32(Console.ReadKey(true).KeyChar) == 13)
                    flag = false;
            }
            orders[Ordernum].Details.OrderDate = DateTime.Now;

        }

        public void DeleteMenu()
        {
            bool flag = true;
            while (flag)
            {
                //
                Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
                Console.WriteLine("                1.删除客户\n");
                Console.WriteLine("                2.删除订单\n");
                Console.WriteLine("                3.删除商品\n");
                Console.WriteLine("                4.退出        \n");
                Console.WriteLine("************************************************* \n");
                Console.Write("您的选择是(请输入单个命令前的序号数字)：");
                char choose = Console.ReadKey(true).KeyChar;
                switch (choose)
                {
                    case '1': DeleteCustomer(); break;
                    case '2': DeleteOrder(); break;
                    case '3': DeleteProduct(); break;
                    case '4': flag = false; break;
                    default: break;
                }
            }
        }
        public void DeleteCustomer()
        {
            ShowAllCustomers();
            Console.Write("选择要删除客户的序号：");
            int num = int.Parse(Console.ReadLine());
            customers.RemoveAt(num);
            Console.Write("成功删除" + ",按任意键继续.....");
            Console.ReadKey(true);
        }
        public void DeleteOrder()
        {
            //
            ShowAllOrder();
            Console.Write("选择删除的订单（选择其序号）：");
            int Ordernum = int.Parse(Console.ReadLine());
            orders.RemoveAt(Ordernum);
            Console.Write("成功删除,按任意键继续.....");
            Console.ReadKey(true);
        }
        public void DeleteProduct()
        {
            ShowAllProducts();
            Console.Write("选择要改变商品的序号：");
            int num = int.Parse(Console.ReadLine());
            products.RemoveAt(num);
            Console.Write("成功删除" + ",按任意键继续.....");
            Console.ReadKey(true);
        }

        public void QueryMenu()
        {
            bool flag = true;
            while (flag)
            {
                //
                Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
                Console.WriteLine("                1.订单号\n");
                Console.WriteLine("                2.商品名称\n");
                Console.WriteLine("                3.客户\n");
                Console.WriteLine("                4.订单金额\n");
                Console.WriteLine("                5.退出\n");
                Console.WriteLine("************************************************* \n");
                Console.Write("您的选择是(请输入单个命令前的序号数字)：");
                char choose = Console.ReadKey(true).KeyChar;
                switch (choose)
                {
                    case '1':
                        //
                        Console.Write("输入查询的订单号：");
                        string id = Console.ReadLine();
                        QueryOrderNumber(id);
                        break;
                    case '2':
                        //
                        Console.Write("输入查询的商品名称：");
                        string gn = Console.ReadLine();
                        QueryGoodName(gn);
                        break;
                    case '3':
                        //
                        Console.Write("输入查询的顾客姓名：");
                        string cn = Console.ReadLine();
                        QueryCustomer(cn); break;
                    case '4':
                        //
                        Console.Write("输入查询的订单金额：");
                        double m = double.Parse(Console.ReadLine());
                        QueryMoney(m); break;
                    case '5': flag = false; break;
                    default: break;
                }
            }
        }

        public void QueryOrderNumber(string id)
        {
            var qon = orders.Where(i => i.Details.Id == id).OrderByDescending(i => i.Money());
            List<Order> ods = qon.ToList();
            //
            Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
            for (int i = 0; i < ods.Count; i++)
                Console.WriteLine(i + ".\n" + ods[i]);
            Console.WriteLine("************************************************* \n");
            Console.Write("按任意键退出...");
            Console.ReadKey(true);
        }
        public bool IfContainGN(Order od, string gn)
        {
            for (int i = 0; i < od.Goods.Count; i++)
                if (od.Goods[i].Name == gn)
                    return true;
            return false;
        }
        public void QueryGoodName(string gn)
        {
            var qon = orders.Where(i => IfContainGN(i, gn)).OrderByDescending(i => i.Money());
            List<Order> ods = qon.ToList();
            //
            Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
            for (int i = 0; i < ods.Count; i++)
                Console.WriteLine(i + ".\n" + ods[i]);
            Console.WriteLine("************************************************* \n");
            Console.Write("按任意键退出...");
            Console.ReadKey(true);
        }
        public void QueryCustomer(string cn)
        {
            var qon = orders.Where(i => i.Details.Customer.Name == cn).OrderByDescending(i => i.Money());
            List<Order> ods = qon.ToList<Order>();
            Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
            for (int i = 0; i < ods.Count; i++)
                Console.WriteLine(i + ".\n" + ods[i]);
            Console.WriteLine("************************************************* \n");
        }
        public void QueryMoney(double m)
        {
            var qon = orders.Where(i => i.Money() >= m).OrderByDescending(i => i.Money());
            List<Order> ods = qon.ToList();
            //
            Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
            Console.WriteLine("大于￥" + m + "的订单有：");
            for (int i = 0; i < ods.Count; i++)
                Console.WriteLine(i + ".\n" + ods[i]);
            Console.WriteLine("************************************************* \n");
            Console.Write("按任意键退出...");
            Console.ReadKey(true);
        }

        public void Export(string filename)
        {
            XmlSerializer xmls = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(filename, FileMode.Create))
            {
                xmls.Serialize(fs, orders);
            }
        }
        public void Import(string filename)
        {
            XmlSerializer xmls = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                orders = xmls.Deserialize(fs) as List<Order>;
            }
        }



        public void Sort()
        {
            var qon = orders.OrderByDescending(i => i.Money());
            List<Order> ods = qon.ToList();
            //
            Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
            for (int i = 0; i < ods.Count; i++)
                Console.WriteLine(i + ".\n" + ods[i]);
            Console.WriteLine("************************************************* \n");
            Console.Write("按任意键退出...");
            Console.ReadKey(true);
        }



        public void MenuShow()
        {
            bool flag = true;
            string filename = "orders.xml";
            while (flag)
            {
                //
                Console.WriteLine("*************** 订 单 管 理 系 统 *************** \n");
                Console.WriteLine("                1.显示所有订单\n");
                Console.WriteLine("                2.新增信息\n");
                Console.WriteLine("                3.修改信息    \n");
                Console.WriteLine("                4.删除订单    \n");
                Console.WriteLine("                5.查询订单    \n");
                Console.WriteLine("                6.排序订单    \n");
                Console.WriteLine("                7.导出订单    \n");
                Console.WriteLine("                8.导入订单    \n");
                Console.WriteLine("                9.退出        \n");
                Console.WriteLine("************************************************* \n");
                Console.Write("您的选择是(请输入单个命令前的序号数字)：");
                char choose = Console.ReadKey(true).KeyChar;
                switch (choose)
                {
                    case '1':
                        //
                        ShowAllOrder();
                        Console.Write("按任意键退出...");
                        Console.ReadKey(true);
                        break;
                    case '2': AddMenu(); break;
                    case '3': ChangeMenu(); break;
                    case '4': DeleteMenu(); break;
                    case '5': QueryMenu(); break;
                    case '6': Sort(); break;
                    case '7':
                        Export(filename);
                        //
                        Console.Write("导出成功\n按任意键退出...");
                        Console.ReadKey(true);
                        break;
                    case '8':
                        Import(filename);
                        //
                        Console.Write("导入成功\n按任意键退出...");
                        Console.ReadKey(true);
                        break;
                    case '9': flag = false; break;
                    default: break;
                }
            }
        }


    }
}
